﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DSkin.Forms;
using DSkin.Controls;
using TY_Hoist.WinApp.Code;
using System.Configuration;
using System.Threading;
using System.Threading.Tasks;
using TY_WinApp.Wrapper;
using System.Runtime.InteropServices;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.UI;

namespace TY_Hoist.WinApp
{
    /// <summary>
    /// 主程序窗体
    /// </summary>
    public partial class MainForm : BaseForm
    {
        /// <summary>
        /// 实例化遥控器
        /// </summary>
        RemoteControl remoteControl;
        /// <summary>
        /// 实例化激光测距传感器
        /// </summary>
        LaserRanger laserRanger;
        bool autoUpDownSwich = false;
        bool processSwich = false;
        bool isAutoTop = false;
        bool isAutoBottom = false;
        Color processBtnBaseColor;
        double currentValue = 0;
        double laserRangerInitValue = 0;
        double trackBarSetup = 0;
        int allFrameCount = 0;
        int currentFrame = 0;
        int startFrame = 0;
        int endFrame=0;
        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// //窗体加载完成事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            CVVideo.dsplay += CVVideo_dsplay;

            //DSkinMessageBox.Show(image.ToString());

            //dSkinPictureBox3.Image = bmp;
            //List<byte> dest = (List<byte>)Marshal.PtrToStructure(ip, typeof(List<byte>));
            //DSkinMessageBox.Show(ex.Message);
            //Bitmap newBitmap = new Bitmap(2596, 600, 4, System.Drawing.Imaging.PixelFormat.Format8bppIndexed, image);
            //dSkinPictureBox3.Image = newBitmap;

            //CvInvoke.cvCopy(image, timg, IntPtr.Zero);
            //GcDev.DelegateGcImageFun GcImageFun = new GcDev.DelegateGcImageFun(GetImage);
            //GcDev.GcRegImageCbFun(1, GcImageFun, Marshal.AllocHGlobal((Int32)1024*1024*20), new IntPtr());
            //try
            //{

            //    remoteControl = new RemoteControl();
            //    laserRanger = new LaserRanger();

            //}
            //catch (Exception ex)
            //{

            //    DSkinMessageBox.Show(ex.Message);
            //}
            //processBtnBaseColor = BtnProcessSwich.BaseColor;
            //FreeStatus();
            //timer首次计时到达前手动执行一次显示
            labDisplayTime.Text = DateTime.Now.ToString("yyyy年MM月dd日 HH点mm分ss秒");
            //启动显示timer
            timer1.Start();

        }

        private void CVVideo_dsplay(string obj)
        {
            dSkinLabel8.Text = obj;
        }

        //public void GetImage(IntPtr image)
        //{

        //    //Task.Factory.StartNew(() =>
        //    //{
        //    //    Image<Bgr, Byte> dest = new Image<Bgr, byte>(dSkinPictureBox3.Size);
        //    //    GcImg.GcProcess(1, dest.Ptr, image);
        //    //});
        //    IntPtr image1 = Marshal.AllocHGlobal((Int32)1024 * 1024 * 20);
        //    //DSkinMessageBox.Show(image.ToString());
        //    GcImg.GcProcess(1, image1, image);
        //    this.Invoke(new Action(() =>
        //    {
                 
        //        //CvInvoke.cvCopy(image, dest.Ptr, IntPtr.Zero);
        //        //Image dest = (Image)Marshal.PtrToStructure(image, typeof(Image));
        //        dSkinPictureBox3.Image = GetBitmap(image1);
        //        //dSkinLabel8.Text = DateTime.Now.ToString("ssfff");

        //    }));
        //}
        //unsafe public Bitmap GetBitmap(IntPtr image)
        //{

        //    Bitmap bmp = new Bitmap(1280, 960, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
        //    var data = bmp.LockBits(new Rectangle(0, 0, 1280, 960), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

        //    byte* srcBmpData = (byte*)image.ToPointer();
        //    byte* dstBmpData = (byte*)data.Scan0.ToPointer();

        //    for (int buc = 0; buc < 1280 * 960; buc++)
        //    {
        //        int currentDestPos = buc * 4;
        //        dstBmpData[currentDestPos] = dstBmpData[currentDestPos + 1] = dstBmpData[currentDestPos + 2] = srcBmpData[buc];
        //    }

        //    bmp.UnlockBits(data);

        //    return bmp;
        //}
        /// <summary>
        /// 时间显示事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            labDisplayTime.Text = DateTime.Now.ToString("yyyy年MM月dd日 HH点mm分ss秒");
        }



        /// <summary>
        /// 自动升降系统UI控制
        /// </summary>
        /// <param name="swich"></param>
        private void AutoUpDownUIControl(bool swich)
        {

          
        }

        /// <summary>
        /// 流程开关UI控制
        /// </summary>
        /// <param name="swich"></param>
        private void ProcessSwichUIControl(bool swich)
        {

        }

        /// <summary>
        /// 结束程序事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void BtnProcessSwich_Click(object sender, EventArgs e)
        {
            RecSuite recSuite = new RecSuite();
            recSuite.ShowDialog();
        }

        private void BtnAutoTop_Click(object sender, EventArgs e)
        {
            var setFrameIndex = currentFrame - (int)this.dSkinNumericUpDown1.Value;
            setFrameIndex = setFrameIndex < 1 ? 1 : setFrameIndex;
            SetImageFrame(setFrameIndex);
            this.dSkinTrackBar1.Value = (int)(setFrameIndex / trackBarSetup);

        }

        private void BtnAutoBottom_Click(object sender, EventArgs e)
        {
            var setFrameIndex = currentFrame + (int)this.dSkinNumericUpDown1.Value;
            setFrameIndex = setFrameIndex > allFrameCount ? allFrameCount : setFrameIndex;
            SetImageFrame(setFrameIndex);
            this.dSkinTrackBar1.Value = (int)(setFrameIndex / trackBarSetup);
        }

        private void BtnRCStop_Click(object sender, EventArgs e)
        {
            AutoUpDownUIControl(false);
        }

        private void BtnRCStart_Click(object sender, EventArgs e)
        {

        }

        private void FreeStatus()
        {

        }

        private void BtnCloseForm_Click(object sender, EventArgs e)
        {

            if (processSwich)
            {
                DSkinMessageBox.Show("关闭系统前请先结束实验流程！");
            }
            else
            {
                if (DSkinMessageBox.Show(this, "确定要关闭系统么？", "提醒", MessageBoxIcon.Information, false, MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    //remoteControl.Clear();
                    ////释放串口
                    //remoteControl.Dispose();

                    //laserRanger.Stop();
                    //laserRanger.Clear();
                    //laserRanger.Dispose();

                    //System.Diagnostics.Process.Start("shutdown.exe", "-s -t 0");
                    this.Close();
                }
            }
        }

        private void btnTop_Click(object sender, EventArgs e)
        {

        }

        private void btnUp_Click(object sender, EventArgs e)
        {

        }

        private void dSkinGroupBox6_Paint(object sender, PaintEventArgs e)
        {

        }

        private void dSkinButton7_Click(object sender, EventArgs e)
        {
            MsgTipBox.ShowDialog("正在打开视频中...",new Action(()=> {
                allFrameCount = CVVideo.Open(@"D:\CamVideo.avi");
                trackBarSetup = allFrameCount / 1000.0;
                this.dSkinTrackBar1.Value = 1;
                SetImageFrame(1);

                dSkinGroupBox1.Enabled = true;
                dSkinGroupBox2.Enabled = true;

            }));
        }

        private void dSkinTrackBar1_ValueChanged(object sender, EventArgs e)
        {
            SetImageFrame((int)(dSkinTrackBar1.Value * trackBarSetup));
        }

        private void dSkinTrackBar1_MouseUp(object sender, MouseEventArgs e)
        {

        }
        public void SetImageFrame(int num)
        {
            currentFrame = num;
            CVVideo.setPictureBoxControlImage(dSkinPictureBox3, currentFrame);
            dSkinLabel4.Text = currentFrame.ToString();

        }

        private void dSkinButton5_Click(object sender, EventArgs e)
        {
            startFrame = currentFrame;
            dSkinLabel6.Text = startFrame.ToString();
        }

        private void dSkinButton8_Click(object sender, EventArgs e)
        {
            endFrame = currentFrame;
            dSkinLabel7.Text = endFrame.ToString();
        }

        private void dSkinButton6_Click(object sender, EventArgs e)
        {
            int JuLi = dSkinRadioButton1.Checked ? 50 : 100;
            double  time = (endFrame - startFrame) / 1000.0;
            double SuLv = JuLi / (time * 100.0);

            DSkinMessageBox.Show(string.Format("燃烧时间为{0}s；\r\n燃烧距离{1}cm；\r\n此物质燃烧速率为{2}m/s；", time,JuLi,SuLv));
        }

        private void dSkinButton9_Click(object sender, EventArgs e)
        {
            Form3 form3 = new Form3();
            form3.ShowDialog();
        }
    }
}
